package com.demo.common.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/*
    项目统一响应结果对象
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class R<T> {
    private Integer code; // 响应状态码
    private String msg; // 响应状态说明
    private T date; // 数据

    /**
     * 操作成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> R<T> OK(T data){
        return R.<T>builder()
                .code(BusinsesEnum.SUCCESS.getCode())
                .msg(BusinsesEnum.SUCCESS.getDesc())
                .date(data)
                .build();
    }
    /**
     * 操作失败
     */
    public static <T> R<T> FAIL(BusinsesEnum businsesEnum){
        return R.<T>builder()
                .code(businsesEnum.getCode())
                .msg(businsesEnum.getDesc())
                .date(null)
                .build();
    }
    public static <T> R<T> FAIL(String errorMsg){
        return R.<T>builder()
                .code(BusinsesEnum.OPERATION_ERROR.getCode())
                .msg(errorMsg)
                .date(null)
                .build();
    }
}
